﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
//#JMJ Create_20241208

namespace IHateWorking.Model
{
    public class Converter_Exist2Bool : IValueConverter
    {
        /// <summary>
        /// 通过变量是否存在来返回布尔值
        /// </summary>
        /// <param name="value">控制变量</param>
        /// <param name="targetType"></param>
        /// <param name="parameter">有 Inverse 参数既视为置逆</param>
        /// <param name="culture"></param>
        /// <returns>布尔值</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool inverse = false;
            if (!(parameter is null) && parameter.ToString() == "Inverse")
            { inverse = true; }

            try
            {
                return inverse ? value == null//置逆：value 不为空则返回 false
                    : value != null;//不置逆：value 不为空则返回 true
            }
            catch (Exception e)
            {
                string ex = e.ToString();
                return Binding.DoNothing;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Binding.DoNothing;
        }
    }
}
